<template>
    <div class="bailBorrower" style="padding-bottom: 20px;">
        <Collapse v-model="openPanel" simple>
            <Panel name="1">
                <span>保证人</span>
                <Button
                    :disabled="isHide"
                    @click.stop="add"
                    style="margin-left: 10px;color: #EB9620;border-color: #EB9620;background-color: transparent;"
                >
                    新增保证人
                </Button>
                <div slot="content">
                    <Table :columns="columns" :data="bailInfo" style="margin:20px 45px 0px" class="action" border>
                        <template slot-scope="{ row, index }" slot="action">
                            <Icon type="ios-clipboard-outline" @click="edit(row)" v-show="!isShowDetails&&!isSendBackToDealer"/>
                            <Icon type="ios-trash-outline" @click="deleteRow(row)"
                                  v-show="!isShowDetails && !row.riskQueriedFlag&&!isSendBackToDealer"/>
                        </template>
                    </Table>
                </div>
            </Panel>
        </Collapse>
        <div v-if="isShowModel">
            <common-info
                ref="bailBorrower"
                @close-model="closeModel"
                :signResult="signResult"
                :custRole="custRole"
                :isBack="isBack"
                :isSuggestRejectF="isSuggestRejectF"
                :tempBusinessType="tempBusinessType"
                :isOldCarDisabled="isOldCarDisabled"
                :certTypeList="certTypeList"
                :maritalStatusList="maritalStatusList"
                :sexList="sexList"
                :isEleSign="isEleSign"
                :highestEducationList="highestEducationList"
                :nationalityList="nationalityList"
                :drivingTypeList="drivingTypeList"
                :unitNatureList="unitNatureList"
                :industryTypeList="industryTypeList"
                :jobsTypeList="jobsTypeList"
                :positionList="positionList"
                :addressTypeList="addressTypeList"
                :houseTypeList="houseTypeList"
                :custRelationList="custRelationList"
                :personalType="personalType"
                :dataList="dataList"
                :details="details"
                :applyNo="applyNo"
                :isReconsider="isReconsider"
                :isSendBackToDealer="isSendBackToDealer"
                @queryInfo="queryInfo"
                :is-show-details="isShowDetails"
            ></common-info>
        </div>
    </div>
</template>
<script>
    import PlaceCascade from "@/components/place-cascade"
    import CommonInfo from "../common-info/commonInfo.vue"
    import {deepClone} from "../../../../../../../libs/utils/ObjectClone";
    import {deleteCustomerInfo} from "../../../../../api/apply-report/personal/customInfo";
    import * as utils from '_p/afs-apply/assets/js/utils.js'

    export default {
        name: "",
        data() {
            return {
                isReconsider: false,
                isSuggestRejectF: false,
                isSendBackToDealer: false,
                isBack: false,
                custRole: '',
                signResult: '',
                isEleSign: false,
                openPanel: "1",
                isHide: false,
                isShowModel: false,
                personalType: "",
                bailInfo: [],
                dataList: [],
                details: '',
                columns: [
                    {
                        title: '客户姓名',
                        render: (h, params) => {
                            return h('span', {
                                style: {
                                    color: "rgba(85,130,245)",
                                    cursor: "pointer"
                                },
                                on: {
                                    click: () => {
                                        this.showDetails(params.row);
                                    }
                                }
                            }, params.row.custName)
                        },
                        key: 'custName'
                    },
                    {
                        title: '证件号码',
                        key: 'certNo'
                    },
                    {
                        title: '年龄',
                        key: 'age'
                    },
                    {
                        title: '与主借人关系',
                        key: 'custRelationName'
                    },
                    {
                        title: '手机号码',
                        key: 'telPhone'
                    },
                    {
                        title: '最高学历',
                        key: 'highestEducationName'
                    },
                    {
                        title: '职业',
                        key: 'jobsTypeName'
                    },
                    {
                        title: "工作年限",
                        key: "workAge"
                    },
                    {
                        title: '操作',
                        slot: 'action',
                        width: 150,
                        align: 'center'
                    }

                ],
            }
        },
        props: {
            tempBusinessType: {
                type: String
            },
            isShowDetails: {
                type: Boolean,
            },
            certTypeList: {
                type: Array,
                required: true,
            },
            maritalStatusList: {
                type: Array,
                required: true,
            },
            sexList: {
                type: Array,
                required: true,
            },
            highestEducationList: {
                type: Array,
                required: true,
            },
            nationalityList: {
                type: Array,
                required: true,
            },
            drivingTypeList: {
                type: Array,
                required: true,
            },
            unitNatureList: {
                type: Array,
                required: true,
            },
            industryTypeList: {
                type: Array,
                required: true,
            },
            jobsTypeList: {
                type: Array,
                required: true,
            },
            positionList: {
                type: Array,
                required: true,
            },
            addressTypeList: {
                type: Array,
                required: true,
            },
            houseTypeList: {
                type: Array,
                required: true,
            },
            custRelationList: {
                type: Array,
                required: true,
            },
            bailObj: {
                type: Object,
                required: true
            },
            applyNo: {
                type: String,
            },
            isOldCarDisabled: {
                type: Boolean,
            },
            isNewCarDisabled: {
                type: Boolean,
            },
        },
        components: {
            PlaceCascade,
            CommonInfo
        },
        created() {
        },
        watch: {
            bailObj: function (val) {
                if (Object.keys(val).length > 0) {
                    this.getBailInfoToComm(val);
                    this.isHide = true;
                    this.isEleSign = true;
                }
            }
        },
        mounted() {
            if (this.isShowDetails == true) {
                this.isHide = true;
            }
        },
        methods: {
            showDetails(row) {
                this.isShowModel = true;
                this.$emit("open-model");
                this.dataList = row;
                this.details = 'details';
            },
            //获取共借人信息
            getBailInfoToComm(val) {
                //获取父组件传来的共借人索引-tempArr
                let tempArr = val;
                let bail = [];
                let isBack = tempArr.isBack;
                let isSuggestRejectF = tempArr.isSuggestRejectF;
                let isSendBackToDealer = tempArr.isSendBackToDealer;
                if (isBack == 'yes') {
                    this.isBack = true;
                }
                if (isSuggestRejectF == 'yes') {
                    this.isSuggestRejectF = true;
                }
                //退回经销商
                if (isSendBackToDealer == 'yes') {
                    this.isSendBackToDealer = true;
                }
                let custBaseInfo = deepClone(tempArr.custBaseInfo);
                let personalInfo = deepClone(tempArr.personalInfo);
                let addressList = deepClone(tempArr.addressList);
                let addressListElement = addressList[0].province + "," + addressList[0].city;
                let addressArr = [];
                //删除地址数组中的第一个数组，因为，第一个数组是客户基本信息内的户籍地址,然后，放入addressInfo中的才是需要的地址信息
                let arrayShift = this.$delete(addressList, 0);
                addressList.forEach((item, index) => {
                    let detailAddress = item.province + "," + item.city;
                    let addressObj = {
                        id: item.id,
                        addressType: item.addressType,
                        detailAddressTemp: detailAddress.split(","),
                        inputDetail: item.detailAddress,
                        houseType: item.houseType,
                        houseStartDate: item.houseStartDate,
                        isDefault: eval(item.isDefault),
                        isShow: false,
                    };
                    if (addressObj.isDefault) {
                        addressObj.isShow = true;
                    } else {
                        addressObj.isShow = false;
                    }
                    addressArr.push(addressObj);
                });
                custBaseInfo.custRelationName = utils.setDataDictVal(this.custRelationList, custBaseInfo.custRelation);
                personalInfo.jobsTypeName = utils.setDataDictVal(this.jobsTypeList, personalInfo.jobsType);
                personalInfo.highestEducationName = utils.setDataDictVal(this.highestEducationList, personalInfo.highestEducation);
                let obj = {
                    id: custBaseInfo.id,
                    personalInfoId: personalInfo.id,
                    custName: custBaseInfo.custName,
                    custRelation: custBaseInfo.custRelation,
                    custRelationName: custBaseInfo.custRelationName,
                    signFailNumber: custBaseInfo.signFailNumber || 0,
                    custRole: custBaseInfo.custRole,
                    telPhone: custBaseInfo.telPhone,
                    spellName: personalInfo.spellName,
                    age: personalInfo.age,
                    highestEducation: personalInfo.highestEducation,
                    highestEducationName: personalInfo.highestEducationName,
                    jobsType: personalInfo.jobsType,
                    jobsTypeName: personalInfo.jobsTypeName,
                    workAge: personalInfo.workAge,
                    nationality: personalInfo.nationality,
                    signResult: custBaseInfo.signResult || '',
                    certEndDate: custBaseInfo.certEndDate,
                    certType: custBaseInfo.certType,
                    certNo: custBaseInfo.certNo,
                    birthday: personalInfo.birthday,
                    sex: personalInfo.sex,
                    unitNature: personalInfo.unitNature,
                    unitName: personalInfo.unitName,
                    maritalStatus: personalInfo.maritalStatus,
                    monthlyIncome: personalInfo.monthlyIncome.toString(),
                    totalIncome: personalInfo.totalIncome,
                    outstandingLoan: personalInfo.outstandingLoan,
                    position: personalInfo.position,
                    industryType: personalInfo.industryType,
                    companyPhone: personalInfo.companyPhone,
                    familyNumbers: personalInfo.familyNumbers,
                    isLongTerm: custBaseInfo.isLongTerm,
                    isLock: custBaseInfo.isLock,
                    addressinfo: addressArr,
                    domicLocation: addressListElement.split(","),
                    riskQueriedFlag: custBaseInfo.riskQueriedFlag,
                }
                if (obj.isLongTerm === false || obj.isLongTerm === "false" && custBaseInfo.certEndDate !== null) {
                    obj.isLongTerm = false;
                    obj.certEndDate = custBaseInfo.certEndDate.toString() || null;
                } else if (obj.isLongTerm === true || obj.isLongTerm === "true") {
                    obj.isLongTerm = true;
                    obj.certEndDate = '';
                }
                if (obj.isLock == '1') {
                    this.isReconsider = true;
                } else {
                    this.isReconsider = false;
                }
                this.signResult = obj.signResult;
                this.custRole = obj.custRole;
                bail.push(obj);
                this.bailInfo = bail;
            },
            //修改担保人信息
            edit(row) {
                this.isShowModel = true;
                this.$emit("open-model");
                this.dataList = row;
                this.details = '';
            },
            //删除担保人信息
            deleteRow(row) {
                deleteCustomerInfo(row).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success("删除成功");
                        this.bailInfo = null;
                        this.isHide = false;
                    }
                });
            },

            constructData() {
                let data = this.bailObj;
                let commonInfo = [];
                console.log("bailObj:", data)
                for (let i = 0; i < data.length; i++) {
                    let tempArr = [];
                    let tempstreet = "";
                    let addressinfo = [];
                    for (let j = 0; j < data[i].addressList; i++) {
                        let otherArr = [];
                        if (data[i].addressList[j].addressType === "户籍") {
                            tempArr.push(data[i].addressList[j].province);
                            tempArr.push(data[i].addressList[j].city);
                            tempArr.push(data[i].addressList[j].county);
                            tempstreet = data[i].addressList[j].tempstreet;
                        } else {
                            otherArr.push(data[i].addressList[j].province);
                            otherArr.push(data[i].addressList[j].city);
                            otherArr.push(data[i].addressList[j].county);
                            let obj = {
                                id: data[i].addressList[j].id,
                                addressType: data[i].addressList[j],
                                detailAddress: otherArr,
                                houseType: data[i].addressList[j].houseType,
                                houseStartDate: data[i].addressList[j].houseStartDate,
                                isDefault: eval(data[i].addressList[j].isDefault),
                                isShow: false,
                            }
                            addressinfo.push(obj);
                        }
                    }
                    let obj = {
                        id: data[i].id,
                        personalInfoId: data[i].personalInfo.id,
                        custRelation: data[i].personalInfo.custRelation || '',
                        custName: data[i].custName || '',
                        spellName: data[i].spellName || '',
                        age: data[i].personalInfo.age || '',
                        certType: data[i].certType || '',
                        certNo: data[i].certNo || '',
                        birthday: data[i].personalInfo.birthday || '',
                        certEndDate: data[i].personalInfo.certEndDate || '',
                        isLongTerm: data[i].personalInfo.isLongTerm || '',
                        domicLocation: tempArr || [],
                        domicDetail: tempstreet,
                        maritalStatus: data[i].personalInfo.maritalStatus || "",
                        sex: data[i].personalInfo.sex || '',
                        nationality: data[i].personalInfo.nationality || '',
                        workAge: data[i].personalInfo.workAge || '',
                        highestEducation: data[i].personalInfo.highestEducation || '',
                        telPhone: data[i].telPhone || '',
                        familyNumbers: data[i].personalInfo.familyNumbers || '',
                        unitName: res.data[i].personalInfo.unitName || '',
                        unitNature: res.data[i].personalInfo.unitNature || '',
                        industryType: res.data[i].personalInfo.industryType || '',
                        jobsType: res.data[i].personalInfo.jobsType || '',
                        position: res.data[i].personalInfo.position || '',
                        companyPhone: res.data[i].personalInfo.companyPhone || '',
                        monthlyIncome: res.data[i].personalInfo.monthlyIncome.toString() || '',
                        isLongTerm: res.data[i].custBaseInfo.isLongTerm || '',
                        totalIncome: res.data[i].personalInfo.totalIncome || '',
                        outstandingLoan: res.data[i].personalInfo.outstandingLoan || '',
                        addressinfo: addressinfo,
                    }
                    if (obj.isLongTerm === "false") {
                        obj.isLongTerm = false;
                    } else if (obj.isLongTerm === "true") {
                        obj.isLongTerm = true;
                        obj.certEndDate = '';
                    }
                    commonInfo.push(obj);
                }
                this.bailInfo = commonInfo;
            },
            add() {
                this.isShowModel = true;
                this.$emit("open-model");
                this.personalType = "03";
                this.dataList = "";
            },
            closeModel(value) {
                this.isShowModel = value;
            },
            queryInfo(personalInfo) {
                this.$emit('queryInfo', personalInfo)
            }
        }
    }
</script>
<style>
    .bailBorrower .ivu-table-header {
        border-top: 1px solid #e8eaec;
    }

    /* .bailBorrower  .ivu-table-small{
        border-right: 1px solid #dcdee2;
        border-left: 1px solid #dcdee2;
    } */
    .bailBorrower .action i {
        font-size: 20px;
        color: #EB9620;
        cursor: pointer;
    }
</style>
<style scoped>
    .bailBorrower button i {
        width: 15px;
        height: 15px;
        display: inline-block;
        background-color: #EB9620;
        color: white;
        border-radius: 50%;
        line-height: 15px;
        margin-left: 5px;
    }
</style>
